import request from '@/config/axios'

export interface PasswdLoginVO {
  loginName: string
  password: string
  captchaId?: string
  captcha?: string
  socialType?: number
  socialCode?: string
  socialState?: string
}

export interface SmsCodeVO {
  mobile: string
  scene: string
}

export interface SmsCodeLoginVO {
  mobile: string
  smsCode: string
}

export interface SocialLoginVO {
  type: number
  code: string
  state: string
}

export interface ResetPasswordVO {
  mobile: string
  smsCode: string
  password: string
}

export type TokenVO = {
  accessToken: string // 访问令牌
  refreshToken: string // 刷新令牌
  userId: string // 用户编号
  expiresTime: number //过期时间
}

export const passwdLogin = (data: PasswdLoginVO) => {
  return request.post({
    url: '/system/auth/passwd-login',
    data,
    extension: {
      ignoreToken: true
      // ignoreRepeatCheck: true,
    }
  })
}

export const sendSmsCode = (data: SmsCodeVO) => {
  return request.post({ url: '/system/auth/send-sms-code', data })
}

export const smsCodeLogin = (data: SmsCodeLoginVO) => {
  return request.post({ url: '/system/auth/sms-code-login', data })
}

export const socialAuthRedirect = (type: number, redirectUri: string) => {
  return request.get({ url: '/system/auth/social-auth-redirect', params: { type, redirectUri } })
}

export const socialLogin = (data: SocialLoginVO) => {
  return request.postOriginal({ url: '/system/auth/social-login', data })
}

export const resetPassword = (data: ResetPasswordVO) => {
  return request.post({ url: '/system/auth/reset-password', data })
}

export const getUserInfo = (): Promise<any> => {
  return request.get({ url: '/system/auth/get-permission-info' })
}

export const logout = (): Promise<any> => {
  return request.post({
    url: '/system/auth/logout',
    extension: { errorSilent: true }
  })
}

export const getCaptcha = (scene: string) => {
  return request.get({
    url: '/system/captcha/get',
    params: { scene },
    timeout: 15000,
    extension: { ignoreToken: true }
  })
}

// 放在这里是整体禁用租户及移除租户模块代码时，确保登录页代码不需要进行调整
export const getTenantByWebsite = (website: string) => {
  return request.get({ url: '/system/tenant/get-by-website', params: { website } })
}

export const getTenantByName = (name: string) => {
  return request.get({ url: '/system/tenant/get-by-name', params: { name } })
}

/**
 * 轻量级检查认证状态的接口，主要用于无法通过service.ts进行接口调用的场景，因此无法进行过期票据自动刷新；这种场景可以先
 * 调用该接口，触发过期票据自动刷新(若票据已过期的话)，然后再执行实际的业务接口，如：员工用户导入场景(StaffImportForm
 * 中的el-upload组件调用员工用户导入接口，就是无法显式调用service.ts的场景，而是通过调用el-upload.submit方法)
 */
export const checkAuth = () => {
  return request.get({ url: '/system/auth/check' })
}
